package com.sczfdf.www.sda.gov.cn.pipeline;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.sczfdf.www.sda.gov.cn.dao.C3P0ConnentionProvider;
import com.sczfdf.www.sda.gov.cn.modo.GovDO;

import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.PageModelPipeline;

public class GovPipeline implements PageModelPipeline<GovDO> {
	
	private static String SQL = "INSERT INTO `webmagic_yaojianju_page` (`webmagic_yaojianju_page`.`KEY`,`webmagic_yaojianju_page`.`name`,createTime,recordName,shengchan_companyName,shengchan_companyAddress,shiji_companyName,shiji_companyAddress,weiShengXuKe,elementString,`webmagic_yaojianju_page`.`explain`,remarkString,historyString)  VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
	private PreparedStatement ps = null;
	private Connection conn = null;

	@Override 
	public synchronized void process(GovDO g, Task task) {
		try {
			conn = C3P0ConnentionProvider.getConnection();
			ps = conn.prepareStatement(SQL);

			/**
			 * 补完
			 */
				ps.setString(1, g.getKEY());
				ps.setString(2, g.getName());
				ps.setString(3, g.getCreateTime());
				ps.setString(4, g.getRecordName());
				ps.setString(5, g.getShengchan_companyName());
				ps.setString(6, g.getShengchan_companyAddress());
				ps.setString(7, g.getShiji_companyName());
				ps.setString(8, g.getShiji_companyAddress());
				ps.setString(9, g.getWeiShengXuKe());
				ps.setString(10, g.getElementString());
				ps.setString(11, g.getExplain());
				ps.setString(12, g.getRemarkString());
				ps.setString(13, g.getHistoryString());

				ps.executeUpdate();
			
			
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("错误SQL语句为:"+ps.toString());
		}finally {
			connClose(conn, ps);
		}
		return;
	}
	
	/**
	 * 关闭链接
	 * @param conn 数据库链接
	 * @param ps 预编译语句
	 */
	public void connClose(Connection conn,PreparedStatement ps){
		if(ps != null){
			try {
				ps.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(conn!=null){
            try {  
                conn.close();  
            } catch (SQLException e) {  
            	e.printStackTrace();  
            }  
        }  
	}
}
